package com.wuwei.elearning.question.controller;

import com.wuwei.elearning.question.service.SettingQuestionsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@RestController
@RequestMapping(value="/elearning/settingQuestions")
@CrossOrigin
public class SettingQuestionsController {

    private final ResourceLoader resourceLoader;

    public SettingQuestionsController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Resource
    private SettingQuestionsService settingQuestionsService;

    /**
     * 根据上课的发动机类型和ATA22章的通讯系统类型进设置考题
     * @param engine 发动机类型，支持的类型有E4和TAE
     * @param ata22Type 通讯系统类型，支持的类型有KAP140系统和GFC700系统
     * @param type 考卷类型 0 是中英文，1是纯中文
     * @return
     */
    @GetMapping("Da42/{engine}/{ata22Type}/{type}")
    public void  settingQuestion(@PathVariable("engine") String engine,@PathVariable("ata22Type") String ata22Type,
                                 HttpServletResponse response,@PathVariable("type") Integer type
    ) throws IOException {
        settingQuestionsService.setQuestions(engine, ata22Type,type);
        //将该文件返回给前端
        // 设置响应的内容类型为 Excel
        response.setContentType("text/plain");

        // 设置响应头，告诉浏览器该文件以附件形式下载
        response.setHeader("Content-Disposition", "attachment; filename=\"output.docx\"");
        try(InputStream inputStream = new FileInputStream(filePath);
            OutputStream outputStream = response.getOutputStream()){
            // 将文件的内容以流的形式写入响应对象
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Value("${word.file.path}")
    private String filePath;

    /**
     * DA42 补差班（TAE补AE)出卷
     * @param response
     */
    @GetMapping("Da42/sup")
    public void settingDa42Sup(HttpServletResponse response){
        settingQuestionsService.settingDa42Sup();
        //将该文件返回给前端
        // 设置响应的内容类型为 Excel
        response.setContentType("text/plain");

        // 设置响应头，告诉浏览器该文件以附件形式下载
        response.setHeader("Content-Disposition", "attachment; filename=\"output.docx\"");
        try(InputStream inputStream = new FileInputStream(filePath);
            OutputStream outputStream = response.getOutputStream()){
            // 将文件的内容以流的形式写入响应对象
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

